package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.dto.GetMealTimeDto;
import com.ocom.common.entity.card.EntityXfMealInfo;
import com.ocom.common.entity.card.EntityXfMealTime;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.web.ConsTimeConfigOnShowRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.XfMealInfoService;
import com.ocom.payconfig.service.XfMealTimeService;
import com.ocom.payconfig.vo.DeleteMealVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MealTimeController {


    @Autowired
    private XfMealTimeService mealTimeService;
    @Autowired
    private XfMealInfoService xfMealInfoService;

    //2.2.获取餐段时间信息(getMealTimeInfo)
    @PostMapping(PayConfigUrl.URLP_PayConfig_GetMealTimeInfo)
    public Result GetMealTimeInfo(@RequestBody ConsTimeConfigOnShowRequest request){
        if (request.getComId() == null ){
            return ResultUtil.error(ResultEnum.Err_203);
        }

        List<EntityXfMealTime> entityXfMealTimes = mealTimeService.GetMealTimeInfo(request.getComId(),request);
        Map<String, Object> map = new HashMap<>();
        List<GetMealTimeDto> list = new ArrayList<>();
        for (EntityXfMealTime entityXfMealTime:entityXfMealTimes) {
            GetMealTimeDto getMealTimeDto = new GetMealTimeDto();

            getMealTimeDto.setId(entityXfMealTime.getId());
            getMealTimeDto.setComId(entityXfMealTime.getComId());
            getMealTimeDto.setMealNo(entityXfMealTime.getMealNo());
            getMealTimeDto.setMealName(entityXfMealTime.getMealName());
            getMealTimeDto.setBeginTime(entityXfMealTime.getBeginTime());
            getMealTimeDto.setEndTime(entityXfMealTime.getEndTime());
            getMealTimeDto.setBeforeHour(entityXfMealTime.getBeforeHour());
            getMealTimeDto.setAllowDays(entityXfMealTime.getAllowDays());
            getMealTimeDto.setAllowMinute(entityXfMealTime.getAllowMinute());
            if (entityXfMealTime.getAccTypeId() != null){
                getMealTimeDto.setAccTypeId(entityXfMealTime.getAccTypeId());
            }
            getMealTimeDto.setPointId(entityXfMealTime.getPointId());
            list.add(getMealTimeDto);
        }
        map.put("total",entityXfMealTimes.size());
        map.put("list",list);
        return ResultUtil.success(map);
    }

    //2.4.添加餐段时间信息(addMealTimeInfo)
    @PostMapping(PayConfigUrl.URLP_PayConfig_AddMealTimeInfo)
    public Result AddMealTimeInfo(@RequestBody @Validated EntityXfMealTime entityXfMealTime){
        if (entityXfMealTime.getComId() < 0 || entityXfMealTime.getMealNo() == null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        return mealTimeService.AddMealTimeInfo(entityXfMealTime.getComId(),entityXfMealTime);
    }

    // 2.6.修改餐段时间信息(updateMealTimeInfo)
    @PostMapping(PayConfigUrl.URLP_PayConfig_UpdateMealTimeInfo)
    public Result UpdateMealTimeInfo(@RequestBody @Validated EntityXfMealTime entityXfMealTime){
        if (  entityXfMealTime.getComId() < 0){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        return mealTimeService.UpdateMealTimeInfo(entityXfMealTime.getComId(),entityXfMealTime);
    }

    /**
     * 2.8.删除餐段时间信息(deleteMealTimeInfo)
     * */
    @PostMapping(PayConfigUrl.URLP_PayConfig_DeleteMealTimeInfo)
    public Result DeleteMealTimeInfo(@RequestBody @Validated DeleteMealVo deleteMealVo){
        if (deleteMealVo.getComId() == null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        try {
            mealTimeService.DeleteMealTimeInfo(deleteMealVo.getComId(),deleteMealVo);
        }catch (Exception e){
            e.printStackTrace();
        }

        return ResultUtil.success();
    }


    // 获取单位餐段列表
    @PostMapping(PayConfigUrl.URLP_PayConfig_getCompanyMealNo)
    public Result<List<EntityXfMealInfo>> getCompanyMealNo(Long comId){
        return ResultUtil.success(xfMealInfoService.GetMealNameInfo(null,comId,null,null));
    }

}
